package vip.xiaonuo.visitordevice.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import vip.xiaonuo.core.context.constant.ConstantContextHolder;
import vip.xiaonuo.core.pojo.response.ResponseData;
import vip.xiaonuo.facedoor.modular.memberuser.entity.MemberUser;
import vip.xiaonuo.facedoor.modular.visitor.entity.Visitor;
import vip.xiaonuo.sys.modular.org.entity.SysOrg;
import vip.xiaonuo.visitordevice.service.VisitorDeviceApiService;
import vip.xiaonuo.visitordevice.vo.VisitorApiSaveVO;
import vip.xiaonuo.visitordevice.vo.VisitorDeviceCountVO;
import vip.xiaonuo.visitordevice.vo.VisitorQueryVO;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@Api("访客机接口")
public class VisitorDeviceApiController {


    @Resource
    private VisitorDeviceApiService visitorDeviceService;

    @ApiOperation(value = "访客机登录", tags = "访客机接口")
    @PostMapping("/visitorDeviceApi/login")
    public ResponseData login(@RequestParam @ApiParam("设备序列号") String serialNumber,
                              @RequestParam @ApiParam("设备登录密码") String passWord) {
        return ResponseData.success(visitorDeviceService.login(serialNumber, passWord));
    }

    @ApiOperation(value = "访客机心跳接口", tags = "访客机接口")
    @PostMapping("/visitorDeviceApi/heartBeat")
    public ResponseData heartBeat(@RequestParam @ApiParam("设备id") Long deviceId) {
        visitorDeviceService.heartBeat(deviceId);
        return ResponseData.success();
    }

    @ApiOperation(value = "访客配置获取接口", tags = "访客机接口")
    @PostMapping("/visitorDeviceApi/getDeviceConfig")
    public ResponseData getDeviceConfig(@RequestParam @ApiParam("设备ip") String ip,
                                        @RequestParam @ApiParam("设备端口") String port,
                                        @RequestParam @ApiParam("设备序列号") String deviceNo) {
        // 获取设备端请求的服务端ip和端口
        String[] split = ConstantContextHolder.getSysConfig("VISITOR_SERVER_IP", String.class, true).split(":");
        return ResponseData.success(visitorDeviceService.getDeviceConfig(ip, port, split[0], split[1], deviceNo));
    }


    @ApiOperation(value = "邀请人模糊查询", tags = "访客机接口", response = MemberUser.class)
    @GetMapping(value = "/visitorDeviceApi/inviterList")
    public ResponseData memberList(@ApiParam("姓名") String name,
                                   @ApiParam("手机号码") String phone,
                                   @ApiParam("组织id") Long orgId) {
        return ResponseData.success(visitorDeviceService.memberList(name, phone, orgId));
    }

    @ApiOperation(value = "被访组织查询", tags = "访客机接口", response = SysOrg.class)
    @GetMapping(value = "/visitorDeviceApi/orgList")
    public ResponseData orgList(@ApiParam("组织姓名") String name) {
        return ResponseData.success(visitorDeviceService.orgList(name));
    }

    @ApiOperation(value = "访客提交并且审批通过", tags = "访客机接口", response = Visitor.class)
    @RequestMapping(value = "/visitorDeviceApi/saveAndApproved", method = RequestMethod.POST)
    public ResponseData saveAndApproved(@RequestBody @Validated VisitorApiSaveVO visitorApiVO){
        return ResponseData.success(visitorDeviceService.saveAndApproved(visitorApiVO));
    }

    @ApiOperation(value = "访客提交", tags = "访客机接口", response = Visitor.class)
    @RequestMapping(value = "/visitorDeviceApi/save", method = RequestMethod.POST)
    public ResponseData save(@RequestBody @Validated VisitorApiSaveVO visitorApiVO)  {
        return ResponseData.success(visitorDeviceService.save(visitorApiVO));
    }

    @ApiOperation(value = "访客离场", tags = "访客机接口")
    @PostMapping(value = "/visitorDeviceApi/leave")
    public ResponseData leave(@ApiParam("是否全部离场") Boolean all
            , @ApiParam("单个访客id") Long visitorId) {
        visitorDeviceService.leave(all, visitorId);
        return ResponseData.success();
    }

    @ApiOperation(value = "统计数据", tags = "访客机接口", response = VisitorDeviceCountVO.class)
    @GetMapping(value = "/visitorDeviceApi/count")
    public ResponseData count(@RequestParam @ApiParam("设备id") Long visitorDeviceId) {
        return ResponseData.success(visitorDeviceService.count(visitorDeviceId));
    }

    @ApiOperation(value = "打印二维码", tags = "访客机接口", response = VisitorDeviceCountVO.class)
    @GetMapping(value = "/visitorDeviceApi/printQrCode")
    public ResponseData printQrCode(@RequestParam @ApiParam("访客id") Long visitorId) {
        return ResponseData.success(visitorDeviceService.printQrCode(visitorId));
    }

    @ApiOperation(value = "制卡", tags = "访客机接口", response = VisitorDeviceCountVO.class)
    @GetMapping(value = "/visitorDeviceApi/mackCard")
    public ResponseData mackCard(@RequestParam @ApiParam("访客id") Long visitorId
            , @RequestParam @ApiParam("卡号") String icCard) {
        return ResponseData.success(visitorDeviceService.mackCard(visitorId, icCard));
    }


    @ApiOperation(value = "访客列表分页查询接口", tags = "访客机接口", response = Visitor.class)
    @PostMapping(value = "/visitorDeviceApi/page")
    public ResponseData page(@RequestBody VisitorQueryVO visitorQueryVO) {
        return ResponseData.success(visitorDeviceService.findPage(visitorQueryVO));
    }


    @ApiOperation(value = "访客详情接口", tags = "访客机接口", response = Visitor.class)
    @GetMapping(value = "/visitorDeviceApi/visitorDetail")
    public ResponseData visitorDetail(@ApiParam("访客id") @RequestParam(required = false) Long visitorId,
                                      @ApiParam("卡号") @RequestParam(required = false) String card) {
        return ResponseData.success(visitorDeviceService.visitorDetail(visitorId, card));
    }

    @ApiOperation(value = "卡号是否已经被占用", tags = "访客机接口", response = Boolean.class)
    @GetMapping(value = "/visitorDeviceApi/existsVisitorCard")
    public ResponseData existsVisitorCard(@ApiParam("卡号") @RequestParam String card) {
        return ResponseData.success(visitorDeviceService.existsVisitorCard(card));
    }


}
